package com.shoppingmall.controller.backstage;

import com.shoppingmall.common.ServletResponse;
import com.shoppingmall.pojo.User;
import com.shoppingmall.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;

/**
 * Created by
 *   商品品类的管理 ----必须要有管理员权限才可以查看
 * @author touni
 * @date 2019/3/19
 * @time 15:54
 */

@RestController
@RequestMapping("/manager/category")
public class ManagerCategoryController {

    @Autowired
    private CategoryService categoryService;



    /**
     * 存储用户的session的名称
     */
    @Value("${USER_SESSION}")
    private String userSessoin;

    /**
     * 定义一个通用的方法，来确认用户是否登录或者是否有权限
     * @param session
     * @return
     */
    public ServletResponse checkManagerRole(HttpSession session){
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession || 0 != userFromSession.getRole()){
            return ServletResponse.createByErrorMessage("请检查您是否登录或者是否有权限");
        }
        return ServletResponse.createBySuccess();

    }






    /**
     *通过parentId来查找该品类下的商品
     * @param parentId
     * @param session
     * @return
     */
    @RequestMapping("/get_category")
    public ServletResponse getCategory(@RequestParam(value = "categoryId",defaultValue = "0")Integer parentId, HttpSession session) {
        //从session中获取用户，并判断是否是管理员
        ServletResponse checkUser = checkManagerRole(session);
        if (!checkUser.isSuccess()){
            return checkUser;
        }

        return categoryService.getCategoryByParentId(parentId);






    }

    /**
     * 增加品类
     * @param session
     * @param parentId
     * @param categoryName
     * @return
     */
    //   todo  后台单测时会出现乱码问题
    @RequestMapping("/add_category")
    public ServletResponse addCategoryIdByParentId(HttpSession session,
            @RequestParam(value = "parentId",defaultValue = "0")Integer parentId,String categoryName){
        //判断用户是否有权限或者登录
        ServletResponse checkUser = checkManagerRole(session);
        if (!checkUser.isSuccess()){
            return checkUser;
        }
        //tip 注意此处需要过滤get乱码
        String str = null;
        try {
            str = new String(categoryName.getBytes(),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }



        return categoryService.addCategoryByParentId(parentId,str);



    }

    /**
     * 更新品类的名称
     * @param session
     * @param categoryId
     * @param categoryName
     * @return
     */
    //   todo  后台单测时会出现乱码问题  暂未过滤get乱码
    @RequestMapping("/set_category_name")
    public ServletResponse resetCategoryName(HttpSession session,Integer categoryId,String categoryName){
        //判断用户是否有权限或者登录
        ServletResponse checkUser = checkManagerRole(session);
        if (!checkUser.isSuccess()){
            return checkUser;
        }
        return categoryService.updateCategoryNameByCategoryId(categoryId,categoryName);



    }


    /**
     * 获取这个节点和这个节点下面所有子节点的数据
     * @return
     */
    @RequestMapping("/get_deep_category")
    public ServletResponse getTheSameParentIdBycategoryId(HttpSession session,Integer categoryId){
        //判断用户是否有权限或者登录
        ServletResponse checkUser = checkManagerRole(session);
        if (!checkUser.isSuccess()){
            return checkUser;
        }
        return  categoryService.getTheSameParentIdByCategoryId(categoryId);

    }





















}
